package xyz.ylx.apirotation.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xyz.ylx.apirotation.service.impl.GeminiProxyServiceImpl;
import xyz.ylx.apirotation.service.impl.OpenAIServiceImpl;

import java.util.List;
import java.util.Optional;

/**
 * AI模型服务工厂
 */
@Component
public class AIModelServiceFactory {
    
    private final List<AIModelService> services;
    
    @Autowired
    public AIModelServiceFactory(GeminiProxyServiceImpl geminiService, OpenAIServiceImpl openaiService) {
        this.services = List.of(geminiService, openaiService);
    }
    
    /**
     * 根据模型名获取合适的服务
     * 
     * @param modelName 模型名称
     * @return 对应的服务实现
     */
    public Optional<AIModelService> getServiceForModel(String modelName) {
        if (modelName == null || modelName.isEmpty()) {
            // 默认使用OpenAI服务
            return services.stream()
                .filter(service -> service instanceof OpenAIServiceImpl)
                .findFirst();
        }
        
        return services.stream()
            .filter(service -> service.supportsModel(modelName))
            .findFirst();
    }
    
    /**
     * 获取默认服务（OpenAI）
     */
    public AIModelService getDefaultService() {
        return services.stream()
            .filter(service -> service instanceof OpenAIServiceImpl)
            .findFirst()
            .orElseThrow(() -> new IllegalStateException("未找到默认服务"));
    }
} 